import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
import store from '@/store'
// import { getToken } from '@/utils/auth'
import { wrap } from '@/utils/tools'
const pkg = require('../../package.json')

const token = 'eyJhbGciOiJIUzI1NiJ9.eyJMTkdPUEVSQVRPUklEIjoiMSIsIklQIjpudWxsLCJMTkdST0xFSUQiOjEsIkxOR0VNUExPWUVFSUQiOi0xLCJTVFJPUEVSQVRPUk5BTUUiOiLns7vnu5_kuLvnrqEiLCIzMTAiOiI1MDUiLCIzMTEiOiI1MDUiLCIzMTIiOiI1MDUiLCJMTkdQUk9EVUNUSUQiOiIzMTUsMzE2LDMxMywzMTAsMzExLDMxMiwzMTQsMzE4IiwiMzEzIjoiNTA1IiwiMzE0IjoiNTA1IiwiMzE1IjoiNTA1IiwiMzE2IjoiMCIsIkxOR09SR0FOSVpFSUQiOiIxIiwiMzE4IjoiNTA1IiwiZXhwIjoxNjc3NDc5MzY1fQ.EJkJMM-IQ_EhksbnpAcFQNbmK1mYil0EZn7CSdV1Cgc'
if (process.env.NODE_ENV === 'development') {
  sessionStorage.setItem('X-Token', token)
}

// create an axios instance
const service = axios.create({
  baseURL: process.env.NODE_ENV === 'development' ? process.env.VUE_APP_BASE_API : `${localStorage.getItem('api') || pkg.baseurl}:39999`, // api 的 base_url
  withCredentials: false, // 跨域请求时发送 cookies
  timeout: 5000 // request timeout
})

// request interceptor
service.interceptors.request.use(
  config => {
    // Do something before request is sent
    if (sessionStorage.getItem('X-Token')) {
      // 让每个请求携带token-- ['X-Token']为自定义key 请根据实际情况自行修改
      // config.headers['X-Token'] = getToken()
      config.headers['X-Token'] = sessionStorage.getItem('X-Token')
    }
    return config
  },
  error => {
    // Do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  /**
     * If you want to get information such as headers or status
     * Please return  response => response
     */
  /**
     * 下面的注释为通过在response里，自定义code来标示请求状态
     * 当code返回如下情况则说明权限有问题，登出并返回到登录页
     * 如想通过 XMLHttpRequest 来状态码标识 逻辑可写在下面error中
     * 以下代码均为样例，请结合自生需求加以修改，若不需要，则可删除
     */
  response => {
    const res = response.data
    if (!res.code) {
      return res
    }
    if (res.code !== 20000 && res.code !== 20007) {
      Message({
        dangerouslyUseHTMLString: true,
        message: wrap(res.message || 'error'),
        type: 'error',
        duration: 5 * 1000
      })
      // 50008:非法的token; 50012:其他客户端登录了;  50014:Token 过期了;
      if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
        // 请自行在引入 MessageBox
        // import { Message, MessageBox } from 'element-ui'
        MessageBox.confirm('你已被登出，可以取消继续留在该页面，或者重新登录', '确定登出', {
          confirmButtonText: '重新登录',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          store.dispatch('user/resetToken').then(() => {
            location.reload() // 为了重新实例化vue-router对象 避免bug
          })
        })
      }
      return Promise.resolve(res)
    } else {
      return res
    }
  },
  error => {
    console.log('err' + error) // for debug
    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

export default service
